﻿/*
 * Copyright (c) 2010 Christoc.com
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this 
 * software and associated documentation files (the "Software"), to deal in the Software
 * without restriction, including without limitation the rights to use, copy, modify, 
 * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to 
 * permit persons to whom the Software is furnished to do so, subject to the following 
 * conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all copies 
 * or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 
 * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE 
 * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 * 
 */

namespace Com.Christoc.HomeController.Admin
{
    using System;
    using System.Web.UI.WebControls;
    using DotNetNuke.Entities.Users;
    using DotNetNuke.Security;
    using DotNetNuke.Services.Localization;

    public partial class RoomList : HomeControllerModuleBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //TODO: add in a confirmation window when clicking on delete in the GridView Rows
            if(!IsPostBack)
                Localization.LocalizeGridView(ref RoomsGridView,LocalResourceFile);
            if(!IsEditable)
            {
                AddRoomButton.Visible = false;
            }
        }

        protected void RoomListDataSource_Inserted(object sender, LinqDataSourceStatusEventArgs e)
        {
            RoomsGridView.DataBind();
        }

        protected void RoomListDataSource_Inserting(object sender, LinqDataSourceInsertEventArgs e)
        {
            var hcr = (HomeController_Room)e.NewObject;
            hcr.CreatedByUserId = UserInfo.UserID;
            hcr.LastUpdatedByUserId = UserInfo.UserID;
            hcr.LastUpdated = hcr.DateCreated = DateTime.Now.ToUniversalTime();
        }

        protected void RoomListDataSource_Updating(object sender, LinqDataSourceUpdateEventArgs e)
        {
            var hcr = (HomeController_Room)e.NewObject;            
            hcr.LastUpdatedByUserId = UserInfo.UserID;
            hcr.LastUpdated = DateTime.Now.ToUniversalTime();
        }

        protected void RoomListDataSource_Selecting(object sender, LinqDataSourceSelectEventArgs e)
        {
            //if you want to include any where parameters here
            //e.WhereParameters["ModuleId"] = ModuleId;
        }

        protected void AddRoomButton_Click(object sender, EventArgs e)
        {
            RoomView.Visible = true;
            RoomsGridView.DataBind();
        }

        protected void RoomsGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if ((e.Row.RowType == DataControlRowType.DataRow))
            {
                var hcr= ((HomeController_Room)(e.Row.DataItem));

                if ((PortalSecurity.IsInRole("Administrators"))
                || (UserController.GetCurrentUserInfo().UserID == hcr.CreatedByUserId))
                {
                    e.Row.Cells[0].Enabled = true;
                }
                else
                {
                    e.Row.Cells[0].Text = string.Empty;
                }
            }
        }

        protected void InsertButton_Click(object sender, EventArgs e)
        {
            RoomView.Visible = false;
            AddRoomButton.Text = Localization.GetString("UpdateSuccessful.Text",LocalResourceFile);
            RoomsGridView.DataBind();
        }

        protected void InsertCancelButton_Click(object sender, EventArgs e)
        {
            RoomView.Visible = false;
            RoomsGridView.DataBind();
        }

    }
}